/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.controllers.HouseController;
import com.aionengine.gameserver.model.gameobjects.HouseDecoration;
import com.aionengine.gameserver.model.gameobjects.PersistentState;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.house.House;
import com.aionengine.gameserver.model.templates.housing.PartType;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_HOUSE_EDIT;
import com.aionengine.gameserver.questEngine.QuestEngine;
import com.aionengine.gameserver.questEngine.model.QuestEnv;

/**
 * @author Rolandas
 */
public class CM_HOUSE_DECORATE extends AionClientPacket {

    int objectId;
    int templateId;
    int lineNr; // Line number (starts from 1 in 3.0 and from 2 in 3.5) of part in House render/update packet

    public CM_HOUSE_DECORATE(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    @Override
    protected void readImpl() {
        objectId = readD();
        templateId = readD();
        lineNr = readH();
    }

    @Override
    protected void runImpl() {
        Player player = getConnection().getActivePlayer();
        if (player == null)
            return;

        House house = player.getHouseRegistry().getOwner();

        PartType partType = PartType.getForLineNr(lineNr);
        int floor = lineNr - partType.getStartLineNr();

        if (objectId == 0) {
            // change appearance to default, delete any applied customs finally
            HouseDecoration decor = house.getRegistry().getDefaultPartByType(partType, floor);
            if (decor.isUsed()) {
                return;
            }
            house.getRegistry().setPartInUse(decor, floor);
        } else {
            // remove from inventory
            HouseDecoration decor = house.getRegistry().getCustomPartByObjId(objectId);
            house.getRegistry().setPartInUse(decor, floor);
            sendPacket(new SM_HOUSE_EDIT(4, 2, objectId)); // yes, in retail it's sent twice!
        }

        sendPacket(new SM_HOUSE_EDIT(4, 2, objectId));
        house.getRegistry().setPersistentState(PersistentState.UPDATE_REQUIRED);
        ((HouseController) house.getController()).updateAppearance();
        QuestEngine.getInstance().onHouseItemUseEvent(new QuestEnv(null, player, 0, 0));
    }

}
